package jnpf.portal.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.EmailApi;
import jnpf.base.vo.PageListVO;
import jnpf.engine.FlowTaskApi;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.model.flowtask.FlowTaskListModel;
import jnpf.engine.model.flowtask.PaginationFlowTask;
import jnpf.message.model.NoticeModel;
import jnpf.message.model.NoticeVO;
import jnpf.util.JsonUtil;
import jnpf.base.ActionResult;
import jnpf.base.vo.ListVO;
import jnpf.message.NoticeApi;
import jnpf.portal.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页控制器
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @date 2019年9月27日 上午9:18
 */
@Tag(name = "主页控制器", description = "Home")
@RestController
@RequestMapping("/Dashboard")
public class DashboardController {
    @Autowired
    private FlowTaskApi flowTaskApi;

    @Autowired
    private NoticeApi noticeApi;

    @Autowired
    private EmailApi emailApi;

    /**
     * 获取我的待办
     *
     * @return
     */
    @Operation(summary = "获取我的待办")
    @PostMapping("/FlowTodoCount")
    public ActionResult getFlowTodoCount(@RequestBody FlowTodo flowTodo) {
        FlowTodoCountVO vo = new FlowTodoCountVO();
        PaginationFlowTask pagination = new PaginationFlowTask();
        pagination.setPageSize(1L);
        pagination.setCurrentPage(1L);
        pagination.setFlowCategory(String.join(",",flowTodo.getToBeReviewedType()));
        PageListVO waitList = flowTaskApi.getWaitList(pagination);
        vo.setToBeReviewed((long)waitList.getPagination().getTotal());

        pagination.setFlowCategory(String.join(",",flowTodo.getFlowDoneType()));
        PageListVO trialList = flowTaskApi.getTrialList(pagination);
        vo.setFlowDone((long)trialList.getPagination().getTotal());

        pagination.setFlowCategory(String.join(",",flowTodo.getFlowCirculateType()));
        PageListVO circulateList = flowTaskApi.getCirculateList(pagination);
        vo.setFlowCirculate((long)circulateList.getPagination().getTotal());

        vo.setEntrust((long)flowTaskApi.getDelegateList().size());
        return ActionResult.success(vo);
    }

    /**
     * 获取通知公告
     *
     * @return
     */
    @Operation(summary = "获取通知公告")
    @PostMapping("/Notice")
    public ActionResult getNotice(@RequestBody NoticeModel noticeModel) {
        List<NoticeVO> list = JsonUtil.getJsonToList(noticeApi.getNoticeList(noticeModel.getTypeList()), NoticeVO.class);
        ListVO<NoticeVO> voList = new ListVO();
        voList.setList(list);
        return ActionResult.success(voList);
    }

    /**
     * 获取未读邮件
     *
     * @return
     */
    @Operation(summary = "获取未读邮件")
    @GetMapping("/Email")
    public ActionResult getEmail() {
        List<EmailVO> list = JsonUtil.getJsonToList(emailApi.getReceiveList(), EmailVO.class);
        ListVO<EmailVO> voList = new ListVO();
        voList.setList(list);
        return ActionResult.success(voList);
    }

    /**
     * 获取待办事项
     *
     * @return
     */
    @Operation(summary = "获取待办事项")
    @GetMapping("/FlowTodo")
    public ActionResult getFlowTodo() {
        PaginationFlowTask pagination = new PaginationFlowTask();
        pagination.setPageSize(10L);
        pagination.setCurrentPage(1L);
        PageListVO pageListVO = flowTaskApi.getWaitList(pagination);
        List<FlowTaskListModel> taskList = JsonUtil.getJsonToList(pageListVO.getList(),FlowTaskListModel.class);
        List<FlowTodoVO> list = new ArrayList<>();
        for (FlowTaskListModel taskEntity : taskList) {
            FlowTodoVO vo = JsonUtil.getJsonToBean(taskEntity, FlowTodoVO.class);
            vo.setTaskNodeId(taskEntity.getThisStepId());
            vo.setTaskOperatorId(taskEntity.getId());
            vo.setType(2);
            list.add(vo);
        }
        ListVO voList = new ListVO<>();
        voList.setList(list);
        return ActionResult.success(voList);
    }

    /**
     * 获取我的待办事项
     *
     * @return
     */
    @Operation(summary = "获取我的待办事项")
    @GetMapping("/MyFlowTodo")
    public ActionResult getMyFlowTodo() {
        PaginationFlowTask pagination = new PaginationFlowTask();
        List<MyFlowTodoVO> list = JsonUtil.getJsonToList(flowTaskApi.getWaitList(pagination), MyFlowTodoVO.class);
        ListVO<MyFlowTodoVO> voList = new ListVO();
        voList.setList(list);
        return ActionResult.success(voList);
    }
}
